package com.zadtek.jpsqishi.frame;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.app.Application;

/**
 * 存储没有一个Activity,实现关闭所有Activity退出程序的操作
 * 
 * @author Administrator
 * 
 */
public class MyApplication extends Application {

	private List<Activity> activityList = new ArrayList<Activity>();
	private static MyApplication instance;
	
	public static boolean isRunBack = false;	//是否运行在后台

	private MyApplication() {

	}

	/**
	 * 单利模式,获取唯一的MyActivity实例对象
	 * 
	 * @return instance
	 */
	public synchronized static MyApplication getInstace() {
		if (null == instance) {
			instance = new MyApplication();
		}
		return instance;
	}

	/**
	 * 添加Activity到容器
	 * 
	 * @param activity
	 */
	public void addActivity(Activity activity) {
		activityList.add(activity);
	}


	public String getTopActivity(){
		return  activityList.get(activityList.size() - 1).getClass().getName();
	}
	/**
	 * 遍历所有活动的activity并finish
	 */
	public void exit() {
		try {
			for (Activity activity : activityList) {
				if (activity != null)
					activity.finish();
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			isRunBack= false;
			System.exit(0);
		}
	}

	public List<Activity> getAllActivity() {
		return activityList;
	}
}
